package com.epam.qanda.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.epam.qanda.db.ConnectionManager;
import com.epam.qanda.db.dao.BaseDao;
import com.epam.qanda.db.mapping.Converter;
import com.epam.qanda.db.model.AnswerUser;

public class AnswerUserDao implements BaseDao {
	private final String GET_ANSWERS = "SELECT a.id, a.question_id,a.user_id,u.login, u.rating, u.status, u.photo,a.text,a.time,a.likes FROM answer a JOIN user u ON a.user_id=u.id AND question_id = ?";
	private static final Logger LOG = Logger.getLogger("stdout");
	
	
	@SuppressWarnings({ "unchecked" })
	public <T> T getAllAnswerUsers(Integer questionId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<AnswerUser> answers = new ArrayList<AnswerUser>();
		try (PreparedStatement statement = connection.prepareStatement(GET_ANSWERS)) {
			statement.setInt(1, questionId);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				answers.add(Converter.<AnswerUser> fromResaultSetToObject(rs, AnswerUser.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) answers;
	}

	@Override
	public <T> void create(T element) {
	}

	@Override
	public <T> T read(Integer id) {
		return null;
	}

	@Override
	public void delete(Integer id) {
	}

	@Override
	public <T> void update(T element) {
	}
}
